<?php

namespace App\Console\Commands\Third\Xiaoke;

use App\Models\Third\Xiaoke\CustomerStageDataModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class FixXkCustomerStageDataCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'xk:fixCustomerStageData';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '销氪:修复历史数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        CustomerStageDataModel::query()->chunk(1000, function (Collection $collection) {
            $collection->each(function (CustomerStageDataModel $customerStageDataModel) {
                $msgBodyArr = json_decode($customerStageDataModel->originalMsgBody, true);
                $customerStageDataModel->setRawAttributes([
                    "remark"         => $msgBodyArr["customer"]["s_remark"] ?? "",
                    "advertiserUid"  => $msgBodyArr["customer"]["c_guanggaozhuid"] ?? 0,
                    "advertiserName" => $msgBodyArr["customer"]["c_guanggaozhumingcheng"] ?? "",
                ])->save();
            });
        });

        return 0;
    }
}
